// pages/gouwuche/gowuche.js

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    totalPrice: 0,
    totalNum: 0,
    allChecked: true,
    carts: [],
    num: 1,
    checkw: true,
    pass: 123456,
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: true, //文本框焦点

  },
  nums: 1,
  handleGetUserInfo() {
    wx.chooseAddress({
      success: (result) => {
        let address = result
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
        wx.setStorageSync('address', address)
      },
    })
  },
  setCart(carts) {
    let totalPrice = 0;
    let totalNum = 0;
    let allChecked = true;
    //先判断数组是否为空
    if (carts.length == 0) {
      allChecked = false
    } else {
      //获取总价格、总数量
      carts.forEach(v => {
        if (v.checked) {
          totalPrice += v.goods_price * v.num;
          totalNum += v.num;
        } else {
          allChecked = false
        }
      })
    }
    this.setData({
      totalPrice,
      totalNum,
      allChecked
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  showInputLayer() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  hidePayLayer() {
    /**获取输入的密码**/
    var val = this.data.pwdVal;
    if (val != this.data.pass) {
      this.setData({
        showPayPwdInput: false,
        payFocus: false,
        pwdVal: ''
      }, function () {
        /**弹框**/
        wx.showToast({
          title: "密码错误"
        })
      });
    } else if (val == this.data.pass) {
      this.setData({
        showPayPwdInput: false,
        payFocus: false,
        pwdVal: ''
      }, function () {
        /**弹框**/
        wx.showToast({
          title: "支付成功",
        })
      });
    }


  },
  /**
   * 获取焦点
   */
  getFocus() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd(e) {
    this.setData({
      pwdVal: e.detail.value
    });

    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }
  },
  jiesuan() {
    if (this.data.totalPrice > 0) {
      this.showInputLayer();
    }

  },
  onLoad(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const address = wx.getStorageSync('address')

    this.setData({
      address
    })
    let carts = wx.getStorageSync('carts') || []
    this.setData({
      carts
    })
    this.setCart(carts)
    let car = wx.getStorageSync('carts')
    console.log(car);
    this.setData({
      carts: car
    })
  },
  handleItemChange(e) {
    console.log(e.currentTarget.dataset.id);
    const goods_id = e.currentTarget.dataset.id
    let {
      carts
    } = this.data
    let index = carts.findIndex(v => v.goods_id == goods_id)
    carts[index].checked = !carts[index].checked
    this.setCart(carts)
  },
  handlechangeNum(e) {
    const goods_id = e.currentTarget.dataset.id
    let {
      carts
    } = this.data
    let index = carts.findIndex(v => v.goods_id == goods_id)



    //当商品数量只有一个，还减的时候
    if (carts[index].num == 1 && e.currentTarget.dataset.operation == "-1") {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '删除',
        content: '确认删除？',
        success: (result) => {
          if (result.confirm) {
            carts.splice(index, 1)
            this.setCart(carts)
            this.setData({
              carts
            })
          }
        }
      })
    } else {
      carts[index].num += e.currentTarget.dataset.operation
      this.setCart(carts)
      this.setData({
        carts
      })
    }
  },
  cancelAll() {
    const carts = this.data.carts
    this.setData({
      allChecked: !this.data.allChecked
    })
    if (this.data.allChecked) {
      console.log(this.data.allChecked);
      carts.forEach(v => {
        v.checked = true
      })
      this.setCart(carts)
      this.setData({
        carts
      })
    } else {
      carts.forEach(v => {
        v.checked = false
      })
      this.setCart(carts)
      this.setData({
        carts
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})